"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : PACKAGE
Case Name   : 回收用户当前模式下除create外其他权限,创建package
Create At   : 2022/04/26
Owner       : @zou_jialiang050
Description :
    1.创建用户
    2.用户回收除create外的其他权限
    3.新建用户身份声明package
    4.新建用户身份定义package body
    5.调用package中存储过程,查询表数据
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.调用成功,返回package中存储过程执行结果
    6.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PACKAGE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user = 'u_package_0037'
        self.table = 't_package_0037'
        self.package = 'p_test_0037'
        self.procedure = 'p_package_0037'

    def test_package(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:创建用户 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user} cascade;
            create user {self.user} with password '{macro.PASSWD_REPLACE}';''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:用户回收除create外的其他权限 expect:成功'
        self.logger.info(step)
        revoke_pri = self.primary_sh.execut_db_sql(f'''
            revoke alter on schema {self.user} from {self.user};
            revoke drop on schema {self.user} from {self.user};
            revoke create any package from {self.user};''')
        self.logger.info(revoke_pri)
        self.assertEqual(revoke_pri.count('REVOKE'), 3, '执行失败' + step)

        step = 'step3:新建用户身份声明package expect:成功'
        self.logger.info(step)
        spec_package = self.primary_sh.execut_db_sql(
            f'''create or replace package {self.package} authid definer is
            var1 text:= 'test';
            var2 text:= 'test1';
            procedure {self.procedure}();
            end {self.package};''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(spec_package)
        self.assertIn(self.Con.create_package_success_msg, spec_package,
                      '执行失败' + step)

        step = 'step4:新建用户身份定义package body expect:成功'
        self.logger.info(step)
        def_package = self.primary_sh.execut_db_sql(
            f'''create or replace package body {self.package} is
            procedure {self.procedure}()
            is
            begin
                drop table if exists {self.user}.{self.table};
                create table {self.user}.{self.table}(c_text text);
                insert into {self.user}.{self.table} values(var1),(var2);
            end;
            end {self.package};''',
            sql_type=f" -U {self.user} -W {macro.PASSWD_REPLACE}")
        self.logger.info(def_package)
        self.assertIn(self.Con.create_package_body_success_msg, def_package,
                      '执行失败' + step)

        step = 'step5:调用package,查询表数据 ' \
               'expect:调用成功,返回package中存储过程执行结果'
        self.logger.info(step)
        invoke_package = self.primary_sh.execut_db_sql(
            f'''call {self.package}.{self.procedure}();
            select * from {self.table};''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(invoke_package)
        self.assertIn('test', invoke_package, '执行失败' + step)
        self.assertIn('test1', invoke_package, '执行失败' + step)

    def tearDown(self):
        step = 'step6:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop user {self.user} cascade;''')
        self.logger.info(clean_environment)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, clean_environment,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
